using Microsoft.Extensions.Logging;
using SocketPorxyServer;
using SuperSocket.Command;

namespace SuperSocketProxy.Commands;

[Command(Key = (int)Command.AuthUserPass)]
public sealed class AuthUserPass : SocketProxyPacketAsyncCommand<Socket5AuthPassPacket>
{
    protected override async ValueTask ExecuteAsync(SocketProxySession session, Socket5AuthPassPacket package, IPacketResponse response)
    {
        if (session.VerifyUser(package.Username, package.Password))
        {
            session.LogInformation($"远程地址：{session.RemoteEndPoint} proxy：{session.LocalEndPoint} 验证账号密码成功");
            
            await response.SendVerifyAsync(verifyResult: true);
        }
        else
        {
            session.LogError($"远程地址：{session.RemoteEndPoint} proxy：{session.LocalEndPoint} 密码错误");
        
            await response.SendVerifyAsync(verifyResult: false);
            
            throw new CloseSessionException();
        }
    }
}